﻿using System;
using System.Data.SqlClient;

namespace VASJ.Model.Helper
{
    public static class ReaderExt
    {
        #region Get Safe String

        public static string SafeGetString(this SqlDataReader reader, string colName)
        {
            if (!(Convert.IsDBNull(reader[colName])))
            {
                return reader[colName].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        #endregion Get Safe String

        #region Get Number

        public static Decimal GetMoney(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Decimal.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return Decimal.Zero;
            }
        }

        public static Int64 GetBigInt(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Int64.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return 0;
            }
        }

        public static Int32 GetInt(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Int32.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return 0;
            }
        }

        public static Int16 GetSmallInt(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Int16.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return 0;
            }
        }

        public static Int16? GetSmallInt2(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Int16.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return Int16.MinValue;
            }
        }

        public static Byte GetTinyInt(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Byte.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return 0;
            }
        }


        public static decimal GetDecimal(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Decimal.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return 0;
            }
        }

        #endregion Get Number

        #region Get GUID

        public static Nullable<Guid> GetUniqueIdentifier(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return new Guid(SafeGetString(reader, colName));
            }
            else
            {
                return null;
            }
        }

        public static Guid GetGuid(string guid)
        {
            if (string.IsNullOrWhiteSpace(guid)) { return Guid.Empty; }
            else
            {
                Guid guidValue;
                if (Guid.TryParse(guid, out guidValue))
                { return guidValue; }
                else { return Guid.Empty; }
            }
        }

        #endregion Get GUID

        #region Get Date Time

        public static DateTime GetDateTime(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.DateTime.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return DateTime.MinValue;
            }
        }

        #endregion Get Date Time

        #region Get Boolean

        public static bool GetBoolean(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Boolean.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return false;
            }
        }

        public static bool? GetBoolean2(this SqlDataReader reader, string colName)
        {
            if (reader[colName] != DBNull.Value)
            {
                return System.Boolean.Parse(SafeGetString(reader, colName));
            }
            else
            {
                return null;
            }
        }

        #endregion Get Boolean
    }
}
